package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Menu_User;
import com.itheima.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @RequestMapping("/queryPage")
    public Result queryPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = menuService.queryPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {
        try {
            Result result = menuService.insertMenu(menu);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加餐单失败!");
        }
    }

    //添加时的数据会写
    @RequestMapping("/findAll")
    public Result findAll() {
        List<Menu> menus = null;
        try {
            menus = menuService.findAllMenu();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_MENU_FAIL, menus);
        }
    }

    //编辑时的数据会写
    @RequestMapping("/findMenuById")
    public Result findMenuById(Integer id) {
        Menu menu = null;
        try {
            menu = menuService.findMenuById(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_MENU_FAIL);
        }

    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu) {
        try {
            return menuService.editMenu(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑餐单失败");
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            return menuService.deleteMenu(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除餐单失败");
        }
    }

    //main.html页面上菜单项展示
    @RequestMapping("/mainmenu")
    public List<Menu_User> mainMenu() {
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (user != null) {
                String username = user.getUsername();
                List<Menu_User> menuList = menuService.findMenu_UserByUsername(username);
                return menuList;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
